<template>
	<view class="pages">		
		<view class="page-wrap">
		 	<view class="search-container">
		 		<view class="search-container-bar" style="padding-right: 20rpx;">
		 			 <uni-icons @click="screeningClick('')" class="search-icons" color="#2D82F6" size="22" type="screening" />
					 <button  type="primary" class="btn_search" @click="addClick()" size="mini">新增</button>
		 		</view>
		 	</view>
		 	<!-- 搜索条件弹出框 -->
			<uni-popup ref="screeningDialog" :animation="true" type="bottom" :mask-click="true">
		 		<view class="screening-dialog">
					<view class="uni-padding-wrap">
						<view class="uni-title uni-common-mt">创建时间</view>
					</view>
					<uni-datetime-picker v-model="searchForm.todayTime" type="date"  />
		 			<view class="uni-padding-wrap">
		 			 	<view class="uni-title uni-common-mt">状态</view>
		 			</view>
					<uni-data-checkbox :multiple="false" v-model="searchForm.status" mode="default" :localdata="facilityType"></uni-data-checkbox>
		 			<view class="button-div">
		 				<button class="resetButton" @click="cancel()">重置</button>
		 				<button class="okButton" @click="search()">完成</button>
		 			</view>
		 	    </view>
		 	</uni-popup>
		</view>
		<scroll-list ref="list" @refresh="refresh">
			<uni-list class="uni-list" v-for="(item , index) in list" :key="index">
				<view>
					<uni-list-item :border="false" :title="'发布时间：'+item.createTime" :clickable="true" @click="listiemClick(item)">
            <view slot="footer">
              <text v-if="item.status == 0 || item.status == 1" style="color: #2D82F6;font-size: 28rpx">{{ item.status | codeFormat(facilityType) }}</text>
              <text v-if="item.status == 2 || item.status == 3" style="color: #808081;font-size: 28rpx">{{ item.status | codeFormat(facilityType) }}</text>
            </view>
					</uni-list-item>
					<uni-list-item :border="false"  :clickable="true" @click="listiemClick(item)">
						<text slot="header" class="diseaseList-header">任务名称：{{item.title}}</text>
					</uni-list-item>
					<uni-list-item :border="false" class="diseaseList">
						<text slot="header">工单类型：{{item.gdTypeName}}</text>
						<view slot="footer" style="display: flex;flex-direction:row;">
						  <button v-if="item.status == 0" class="btn_return" @click="returnClick(item.taskId)">忽略</button>
						  <button v-if="item.status == 1" class="btn_return" style="border: 1px solid #18bc37;color: #18bc37;" @click="finishClick(item.taskId)">完成</button>
						  <button v-if="item.status == 0 || item.status == 1" class="btn_ok" @click="handleClick(item.taskId)">处置</button>
						</view>
					</uni-list-item>
				</view>
			</uni-list>
		</scroll-list>
		
		<uni-popup ref="popupEdit" type="dialog">
			<view class="uni-popup-dialog">
				<view class="title">
					<text class="text">忽略</text>
				</view>
				<view class="content">
					<uni-forms ref="baseForm">
						<uni-easyinput type="textarea" style="width: 250px;" v-model="inputValue" placeholder="请输入备注" :focus="true" />
					</uni-forms>
				</view>
				<view class="uni-dialog-button-group">
					<view class="button" @click="closeDialog">
						<text>取消</text>
					</view>
					<view class="button border-left" @click="onOk">
						<text class="text">确定</text>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>


<script>
	import { mapGetters } from 'vuex'
	import { getList,update,getgdTaskFile,addReply } from '@/api/gdTask.js'
	export default {
		components: {},
		computed:{ ...mapGetters({ userInfo: 'userInfo' }) },
		data() {
			return {
				selectId:0,
				inputValue:'',
				list:[],				
				facilityType:[{ 'code':0,'codeDesc':'未处理' },{ 'code':1,'codeDesc':'处置中' },{ 'code':2,'codeDesc':'完成' },{ 'code':3,'codeDesc':'忽略' }],
				searchForm:{
					pageNum:1,
					pageSize:15,
					status:'',
				}
			}
		},
		onShow() {
			this.list = []
			this.refresh({ page: 1, size: 10 })
		},	
		methods: {
			getListData(paging){
				return new Promise((resolve, reject)=>{
					this.searchForm.principalId = this.userInfo.userId
					this.searchForm.pageNum = paging.page
					this.searchForm.pageSize = paging.size
					getList(this.searchForm).then(res => {
						resolve(res)
					}, err => {
						reject(err)
					})
				})
			},
			//加载list
			load(paging) {
				this.getListData(paging).then(res =>{
					this.list = [...this.list, ...res.rows]
					this.$refs.list.loadSuccess({ list: this.list, total: res.total })
				}).catch(err =>{
					this.$refs.list.loadFail()
				})
			},
			//刷新list
			refresh(paging) {
				this.getListData(paging).then(res =>{
					this.list = res.rows
					this.$refs.list.refreshSuccess({ list: this.list, total: res.total })
				}).catch(err =>{
					this.$refs.list.refreshFail()
				})
			},
			search() {
				this.$refs.screeningDialog.close()
				this.$refs.list.refresh()
			},
			screeningClick(){
				this.$refs.screeningDialog.open()
			},
			addClick(){
				uni.navigateTo({ url: './edit', })
			},
			listiemClick(item){
				getgdTaskFile({ 'taskId':item.taskId }).then(res => {
					item.pics = res.rows
					uni.setStorage({
						key:'gdTaskFile',
					    data:item,
					    success:()=>{
							uni.navigateTo({ url: './edit?id='+item.taskId })
					    }
					})
				})
			},
			cancel(res) {
				this.searchForm.status = ''
				this.searchForm.todayTime = null
			},
			handleClick(id){
				uni.navigateTo({ url: './handle?id='+id })
			},
			finishClick(id){
				this.selectId = id
				uni.showModal({
					title: '提示',
					content: '是否确认完成?',
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							update({ taskId:id,status:2 }).then(out => {
								uni.showToast({
									title: '完成工单',
									duration: 1000
								})
								this.$refs.list.refresh()
							})
						}
					},
					fail: () => {},
					complete: () => {}
				})
			},
			returnClick(id){
				this.inputValue = ''
				this.selectId = id
				this.$refs.popupEdit.open()	
			},
			closeDialog() {
				this.$refs.popupEdit.close()
			},
			onOk() {
				if(this.inputValue == ''){
					uni.showToast({
						title: '请输入忽略原因',
						duration: 1000,
						icon: 'none'
					})
					return false
				}
				let postdata = { taskId: this.selectId,replyFileList:[],content: '忽略原因：'+this.inputValue }
				addReply(postdata).then(res => {
					if (res.code == 200) {
						update({ taskId:this.selectId,status:3 }).then(respon => {})
						uni.showToast({
							icon: 'none',
							title: '忽略成功！',
							duration: 2000
						})
						this.$refs.list.refresh()
					} 
				})
				this.$refs.popupEdit.close()
			},
		}
	}
</script>
<style lang="scss">
	@import '@/style/list.scss';
</style>
<style lang="scss">
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	/* #endif */
</style>